package com.guigu.www.service.imp;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.www.mapper.GatherDetailsMapper;
import com.guigu.www.pojo.Gather;
import com.guigu.www.pojo.GatherDetails;
import com.guigu.www.service.GatherDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GatherDetailsServiceImpl extends ServiceImpl<GatherDetailsMapper, GatherDetails> implements GatherDetailsService {
    //获取map
    @Autowired
    GatherDetailsMapper gatherDetailsMapper;
    @Override
    public boolean addGathrDetailsd(List<GatherDetails> gatherDetails) {
        int i=0;
        for (GatherDetails gatherDetail : gatherDetails){
            //设置状态K002-1: 已登记
            //K002-2: 已调度
            gatherDetail.setGatherTag("K002-1");
            i=i+gatherDetailsMapper.insertgatherDetail(gatherDetail);
        }

        boolean f=false;
        if(i==gatherDetails.size()){
            f=true;
        }
        return f;
    }
    //根据入库单编号查询当前入库要调度的产品
    @Override
    public GatherDetails queryGatherDetailsByproductId(String productId,int ids) {
        return gatherDetailsMapper.queryGatherDetailsByproductId(productId,ids);
    }
    //修改明细表为K002-2: 已调度
    @Override
    public boolean updateGatherDetailsByproductId(String gatheredAmount,String productId) {
        return gatherDetailsMapper.updateGatherDetailsByproductId(gatheredAmount,productId)>0?true:false;
    }

    @Override
    //登记修改明细表状态
    public boolean updateWarehosingGaherDetils(List<GatherDetails> gatherDetails,int id) {
        int i=0;
        boolean f=false;
        for (GatherDetails gatherDetail : gatherDetails) {
            //调用方法
            i=i+gatherDetailsMapper.updateWarehosingGaherDetils(gatherDetail,id);
        }
        if(gatherDetails.size()==i){
            f=true;
        }
        return f;
    }

    @Override
    //查询当前入库的产品明细
    public GatherDetails querygatherDetailsBycpidByid(int id, String productId) {
        return gatherDetailsMapper.querygatherDetailsBycpidByid(id,productId);
    }

    @Override
    public boolean updatewarehousingexaminequeryGatherByGathers(List<GatherDetails> gatherDetails, Integer id) {
        boolean f=false;
        int i=0;
        for (GatherDetails gatherDetail : gatherDetails) {
            //判断
            if(Double.parseDouble(gatherDetail.getAmount())==Double.parseDouble(gatherDetail.getGatheredAmount())){
                gatherDetail.setGatherTag("K002-4");
            }else {
                gatherDetail.setGatherTag("K002-2");
            }
            //设置已经入库数量
            gatherDetail.setWarehousing((int) (gatherDetail.getWarehousing()+Double.parseDouble(gatherDetail.getGatheredAmount())));
            //修改明细表数据
            i+=gatherDetailsMapper.updatewarehousingexaminequeryGatherByGathers(gatherDetail,id);
        }
        if(f){
            f=true;
        }
        return f;
    }

    @Override
    public PageInfo<GatherDetails> queryBydynamicinventiryall(int pageno, int pagesize, String productId) {
        //获取分页
        PageHelper.startPage(pageno,pagesize);
        List<GatherDetails> list=gatherDetailsMapper.queryBydynamicinventiryall(productId);
        //创建pageinfo
        PageInfo<GatherDetails>pageInfo=new PageInfo<GatherDetails>(list);
        return pageInfo;
    }


}
